package com.glsc.ngateway.opmanage.controller.outsys;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemAuthTemplate;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemInfo;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemTemplateItem;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemTemplateRel;
import com.glsc.ngateway.opmanage.dto.OutSystemQueryDto;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemAuthTemplateService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemTemplateItemService;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemTemplateRelService;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 外部系统权限管理
 */
@RestController
@RequestMapping("/api/outSystem/auth/template")
@Slf4j
public class OutSystemAuthTemplateController {

    @Resource
    private OutSystemAuthTemplateService authTemplateService;

    @Resource
    private OutSystemService outSystemService;

    @Resource
    private OutSystemTemplateRelService relService;

    @Resource
    private OutSystemTemplateItemService itemService;

    /**
     * 分页查询
     *
     * @param outSystemQueryDto
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<OutSystemAuthTemplate>> findClientPage(@RequestBody OutSystemQueryDto outSystemQueryDto) {

        Map<String, Object> param = new HashMap<>();
        if (Objects.nonNull(outSystemQueryDto.getSysId())) {
            param.put("EQ_sysId", outSystemQueryDto.getSysId());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getAuthName1())) {
            param.put("LIKE_authName1", outSystemQueryDto.getAuthName1());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getAuthName2())) {
            param.put("LIKE_authName2", outSystemQueryDto.getAuthName2());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getAuthName3())) {
            param.put("LIKE_authName3", outSystemQueryDto.getAuthName3());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getAuthName4())) {
            param.put("LIKE_authName4", outSystemQueryDto.getAuthName4());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getAuthName5())) {
            param.put("LIKE_authName5", outSystemQueryDto.getAuthName5());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getDeptName())) {
            param.put("LIKE_deptName", outSystemQueryDto.getDeptName());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getPostName())) {
            param.put("LIKE_postName", outSystemQueryDto.getPostName());
        }
        if (StringUtils.isNotBlank(outSystemQueryDto.getOperatorName())) {
            param.put("LIKE_operatorName", outSystemQueryDto.getOperatorName());
        }
        Page<OutSystemAuthTemplate> page = authTemplateService.page(PageUtil.initPageNo(outSystemQueryDto.getPageNo()), PageUtil.initPageSize(outSystemQueryDto.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据客户id查询客户信息
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<OutSystemAuthTemplate> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(authTemplateService.findById(id));
    }

    /**
     * 保存
     *
     * @param outSystemAuthInfo
     * @return
     */
    @Transactional

    @PostMapping("/save")
    public PlatformResponse<OutSystemAuthTemplate> save(@RequestBody OutSystemAuthTemplate outSystemAuthInfo) {

        OutSystemInfo system = outSystemService.findSystemById(outSystemAuthInfo.getSysId());
        outSystemAuthInfo.setSystemName(system.getSysName());
        OutSystemAuthTemplate save = authTemplateService.save(outSystemAuthInfo);

        List<OutSystemTemplateRel> templateRelList = relService.findBySystemId(system.getId());
        List<OutSystemTemplateItem> res = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(templateRelList)) {
            for (OutSystemTemplateRel outSystemTemplateRel : templateRelList) {
                res.add(OutSystemTemplateItem.builder()
                        .templateId(outSystemTemplateRel.getTemplateId()).authId(save.getId()).sysId(system.getId()).status("N").deleted("N").build());
            }
        }

        if (CollectionUtils.isNotEmpty(res)) {
            itemService.saveAll(res);
        }

        return PlatformResponse.successData(save);
    }

    /**
     * 删除接口
     *
     * @return
     */
    @Transactional
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody OutSystemAuthTemplate outSystemAuthInfo) {
        authTemplateService.deleteById(outSystemAuthInfo.getId());

        List<OutSystemTemplateItem> items = itemService.listByAuthId(outSystemAuthInfo.getId());
        items.forEach(e ->e.setDeleted("Y"));
        itemService.saveAll(items);
        return PlatformResponse.success();
    }


}
